package com.compty.paint.web.front;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.compty.core.framework.util.StringUtil;
import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.AuditState;
import com.compty.paint.domain.CatalogType;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.Painting;
import com.compty.paint.domain.SpecialSubject;
import com.compty.paint.domain.UploadState;
import com.compty.paint.manager.AuditStateManager;
import com.compty.paint.manager.CatalogTypeManager;
import com.compty.paint.manager.PainterManager;
import com.compty.paint.manager.PaintingManager;
import com.compty.paint.manager.SpecialSubjectManager;
import com.compty.paint.manager.UploadStateManager;
import com.compty.paint.util.Constants;

/**
 * @Todo:画家Action
 * @CreateTime:2011-6-3 上午11:00:33
 */
public class PainterAction extends BaseAction<Painter> {

	private static final long serialVersionUID = -2568343838881818829L;

	private PainterManager painterManager;
	private CatalogTypeManager  catalogTypeManager;
	private PaintingManager paintingManager;
	private UploadStateManager uploadStateManager;
	private AuditStateManager auditStateManager;
	private SpecialSubjectManager specialSubjectManager;
	
	private Painter painter;
	private List<Painting> paintingList;
	private String paintertype; //画种(油画、国画)
	private String decade; //画家年代及派别 1：近现代国画名家 2:近现代油画名家3:当代国画名家4:当代油画名家5:古代国画名家6:国外名家
	private SpecialSubject specialSubject;//画家专题
	
	public PainterAction() {
		super();
		super.pageSize = 30;
	}
	
	/**
	 *@Todo:列表(分页)形式展现画家
	 *@CreateTime:2011-8-30 下午05:57:27
	 * @throws Exception
	 */
	public String list() throws Exception {
		
        Criteria criteria;

        if (sort != null) {
            boolean isAsc = dir.equalsIgnoreCase("asc");
            criteria = painterManager.createCriteria(sort, isAsc);
        } else {
            criteria = painterManager.createCriteria();
        }
        criteria.addOrder(Order.desc("id"));
        
        if(paintertype != null && "guohua".equals(paintertype)){//判断画种(国画)
        	  CatalogType catalogType  = catalogTypeManager.findUniqueBy("english_name", Constants.CatalogType_Guohua); //所属大类
              if(catalogType == null)
              	return ERROR;
              criteria = criteria.add(Restrictions.eq("catalogType", catalogType));
        }else if(paintertype != null && "youhua".equals(paintertype)){//判断画种(油画)
        	 CatalogType catalogType  = catalogTypeManager.findUniqueBy("english_name", Constants.CatalogType_Youhua); //所属大类
             if(catalogType == null)
             	return ERROR;
             criteria = criteria.add(Restrictions.eq("catalogType", catalogType));
        }
        
        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态正常
        criteria = criteria.add(Restrictions.eq("uploadState", uploadState)); 
        AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核状态通过
        criteria = criteria.add(Restrictions.eq("auditState", auditState)); 
        
        criteria.add(Restrictions.eq("type", "1"));//1:表示客座画家

        this.page = painterManager.pagedQuery(criteria, curPage, pageSize);
        
       return "listAll";
	}
	
	/**
	 *@Todo:名家列表
	 *@CreateTime:2011-8-18 上午10:31:18
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listPainterStar(){
		
		int pageSize = 1000;
		
		Page page = painterManager.findPainterStarByPage(curPage, pageSize, "1");
        List<Painter> list1 = (List<Painter>) page.getResult();
        
        page = painterManager.findPainterStarByPage(curPage, pageSize, "2");
        List<Painter> list2 = (List<Painter>) page.getResult();
        
        page = painterManager.findPainterStarByPage(curPage, pageSize, "3");
        List<Painter> list3 = (List<Painter>) page.getResult();
        
        page = painterManager.findPainterStarByPage(curPage, pageSize, "4");
        List<Painter> list4 = (List<Painter>) page.getResult();
        
        page = painterManager.findPainterStarByPage(curPage, pageSize, "5");
        List<Painter> list5 = (List<Painter>) page.getResult();
        
        request.setAttribute("list1", list1);
        request.setAttribute("list2", list2);
        request.setAttribute("list3", list3);
        request.setAttribute("list4", list4);
        request.setAttribute("list5", list5);
        
		return "listPainterStar";
	}
	
	/**
	 * 根据时期查询名家
	 * @param type: 1表示当代 2表示近代 3表示古代
	 * @return
	 */
	public String listPainterStarByperiod(){
		
		String type = request.getParameter("type");
		
		List<Painter> painterList = painterManager.findPaintersByPeriod(type);
		request.setAttribute("painterList", painterList);
		request.setAttribute("period", type);
		
		return "painterstar_by_period";
	}

	/**
	 * 艺术家详情
	 *@CreateTime:2011-6-22 下午05:47:05
	 */
	public String painterDetail() throws Exception {
		
    	String  idStr = request.getParameter("id");
    	if( !StringUtil.isDigit(idStr) ){
    		return ERROR;
    	}
    	Long id = Long.valueOf(idStr);
		this.painter = painterManager.get(id);
		
		String type = this.painter.getType();
		if("2".equals(type)){//名家
			return "jumpToPainterStar_detail";
		}
		
		
		SpecialSubject special = specialSubjectManager.findByPainterId(id);
		if( special != null){
			this.specialSubject = special;
		}
		
		this.paintingList = paintingManager.findAvailablePaintingsByPainter(painter.getId());
		
		return "painter_detail";
	}
	
	/**
	 * 名家资料详情
	 *@CreateTime:2011-6-22 下午05:47:05
	 */
	public String painterStarDetail() throws Exception {
		
    	String  idStr = request.getParameter("id");
    	if( !StringUtil.isDigit(idStr) ){
    		return ERROR;
    	}
    	Long id = Long.valueOf(idStr);
    	
		this.painter = painterManager.get(id);
		this.paintingList = paintingManager.findAvailablePaintingsByPainter(painter.getId());
		
		return "painterStar_detail";
	}
	
	/**
	 *@Todo:根据年代查询名家
	 *@CreateTime:2011-9-6 下午02:31:16
	 */
	@SuppressWarnings("unchecked")
	public String listPaintersByDecade() {
		
		String decade = request.getParameter("decade");
			
		List<Painter> painterList = painterManager.findPaintersByDecade(decade);
		List<Map> painterReturnList = new ArrayList<Map>();
		for (Painter p : painterList) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", p.getId());
			map.put("name", p.getName());
			map.put("imageid", p.getImageInfo().getId());
			map.put("suffix", p.getImageInfo().getSuffix());
			painterReturnList.add(map);
		}
		
		String  ajaxData_painterList = JSONArray.fromObject(painterReturnList).toString();
		request.setAttribute("ajaxData", ajaxData_painterList);
		
		return "listPaintersByDecade";
	}
	
	/**
	 * 检查英文名是否重复
	 */
	public String checkEnglishName(){
		String englishName = request.getParameter("englishName");
		String painter_id = request.getParameter("painter_id");
		boolean existsFlag = false;
		List<Painter> painterList = painterManager.findbyEnglishName(englishName);
		if( painterList != null && painterList.size()>0 ){
			String ajaxData = "{code:\"0001\",message:\"系统已存在如下重名汉语拼音：";
			for (int i = 0; i < painterList.size(); i++) {
				Painter p = painterList.get(i);
				if( painter_id != null && painter_id.equals(String.valueOf(p.getId()))){
					continue;
				}
				ajaxData += p.getName()+"，"+p.getEnglishName()+"；";
				existsFlag = true;//存在重名
			}
			ajaxData +="请使用新的汉语拼音\"}";
			if(existsFlag){
				request.setAttribute("ajaxData", ajaxData);
			}else{
				request.setAttribute("ajaxData", "{code:\"0000\",message:\"no person found.\"}");
			}
		}else{
			request.setAttribute("ajaxData", "{code:\"0000\",message:\"no person found.\"}");
		}
		return "checkEnglishName";
	}
	
	/**
	 *@Todo:搜索画家
	 *@CreateTime:2011-9-6 下午02:46:47
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchPainter(){
		
		String name = request.getParameter("name");
		try {
			name = URLDecoder.decode(name,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String type = request.getParameter("type");
		
		String hql = "from Painter where name = ? and type = ?";
		List<Painter> painterList = painterManager.find(hql, name,type);
		
		if(painterList== null || painterList.size() < 1){
			
			return "noResult";
			
		}else{
			this.painter = painterList.get(0);
			return "searchResult";
		}
	}

	public void setPainterManager(PainterManager painterManager) {
		this.painterManager = painterManager;
	}

	public void setCatalogTypeManager(CatalogTypeManager catalogTypeManager) {
		this.catalogTypeManager = catalogTypeManager;
	}
	
	public void setPaintingManager(PaintingManager paintingManager) {
		this.paintingManager = paintingManager;
	}

	public void setUploadStateManager(UploadStateManager uploadStateManager) {
		this.uploadStateManager = uploadStateManager;
	}

	public void setAuditStateManager(AuditStateManager auditStateManager) {
		this.auditStateManager = auditStateManager;
	}

	public Painter getPainter() {
		return painter;
	}

	public void setPainter(Painter painter) {
		this.painter = painter;
	}

	public List<Painting> getPaintingList() {
		return paintingList;
	}

	public void setPaintingList(List<Painting> paintingList) {
		this.paintingList = paintingList;
	}

	public String getPaintertype() {
		return paintertype;
	}

	public void setPaintertype(String paintertype) {
		this.paintertype = paintertype;
	}

	public String getDecade() {
		return decade;
	}

	public void setDecade(String decade) {
		this.decade = decade;
	}

	public void setSpecialSubjectManager(SpecialSubjectManager specialSubjectManager) {
		this.specialSubjectManager = specialSubjectManager;
	}

	public SpecialSubject getSpecialSubject() {
		return specialSubject;
	}

	public void setSpecialSubject(SpecialSubject specialSubject) {
		this.specialSubject = specialSubject;
	}
	
}
